package com.hongpu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hongpu.config.SecurityContextUtil;
import com.hongpu.domain.SysUserRole;
import com.hongpu.dto.SysUserRoleDto;
import com.hongpu.mapper.SysUserRoleMapper;
import com.hongpu.service.SysUserRoleService;
import com.hongpu.utils.ResultDto;
import com.hongpu.vo.SysRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Michael zhang
 * @description 针对表【sys_user_role(用户角色表)】的数据库操作Service实现
 * @createDate 2025-06-04 09:00:05
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole>
        implements SysUserRoleService {
    @Autowired
    SysUserRoleMapper baseMapper;

    /**
     * 多个用户设置同一个角色
     *
     * @param sysUserRole
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDto multipleUsersSetTheSameRole(SysUserRoleDto sysUserRoleDto) {
        try {
            if (sysUserRoleDto.getUserIds() != null && sysUserRoleDto.getRoleId() != null) {
                for (Long userId : sysUserRoleDto.getUserIds()) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(userId);
                    sysUserRole.setRoleId(sysUserRoleDto.getRoleId());
                    save(sysUserRole);
                }
            }
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail();
        }
    }

    /**
     * 根据用户ID和角色ID删除该用户的角色
     *
     * @param sysUserRole
     */
    @Override
    public ResultDto deleteRoleByUserId(SysUserRole sysUserRole) {
        sysUserRole.setUpdateBy(SecurityContextUtil.getUserId());
        return baseMapper.deleteRoleByUserId(sysUserRole) == true ? ResultDto.ok() : ResultDto.fail();
    }

    /**
     * 多个用户设置删除同一个角色
     *
     * @param sysUserRoleDto
     * @return
     */
    @Override
    public ResultDto multipleUsersDeleteTheSameRole(SysUserRoleDto sysUserRoleDto) {
        try {
            if (sysUserRoleDto.getUserIds() != null && sysUserRoleDto.getRoleId() != null) {
                for (Long userId : sysUserRoleDto.getUserIds()) {
                    SysUserRole sysUserRole = new SysUserRole();
                    sysUserRole.setUserId(userId);
                    sysUserRole.setRoleId(sysUserRoleDto.getRoleId());
                    sysUserRole.setUpdateBy(SecurityContextUtil.getUserId());
                    baseMapper.deleteRoleByUserId(sysUserRole);
                }
            }
            return ResultDto.ok();
        } catch (Exception e) {
            return ResultDto.fail();
        }
    }

    /**
     * 根据用户ID查询角色ID
     *
     * @param userId
     */
    @Override
    public List<SysRoleVo> getRoleIdsByUserId(Long userId) {
        if (userId == null) {
            return null;
        }
        try {
            List<SysRoleVo> roleIds = baseMapper.getRoleIdsByUserId(userId);
            return roleIds;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 根据用户ID删除所有角色ID
     *
     * @param userId
     */
    @Override
    public boolean deleteRoleIdsByUserId(Long userId) {
        return baseMapper.deleteRoleIdsByUserId(userId);
    }
}




